import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/**
 * The GUI pane where a user books tickets for movie performances. It contains
 * one list of movies and one of performance dates. The user selects a
 * performance by clicking in these lists. The performance data is shown in the
 * fields in the right panel. The bottom panel contains a button which the user
 * can click to book a ticket to the selected performance.
 */
public class StoragePane extends BasicPane {
	/**
	 * A label showing the name of the current user.
	 */
	private JLabel currentUserNameLabel;

	/**
	 * The list model for the movie name list.
	 */
	private DefaultListModel nameListModel;

	/**
	 * The movie name list.
	 */
	private JList nameList;

	/**
	 * The list model for the performance date list.
	 */
	private DefaultListModel dateListModel;

	/**
	 * The performance date list.
	 */
	private JList dateList;

	/**
	 * The text fields where the movie data is shown.
	 */
	private JTextField[] fields;

	/**
	 * The number of the movie name field.
	 */
	private static final int MOVIE_NAME = 0;

	/**
	 * The number of the performance date field.
	 */
	private static final int PERF_DATE = 1;

	/**
	 * The number of the movie theater field.
	 */
	private static final int THEATER_NAME = 2;

	/**
	 * The number of the 'number of free seats' field.
	 */
	private static final int FREE_SEATS = 3;

	/**
	 * Create the booking pane.
	 * 
	 * @param db
	 *            The database object.
	 */

	ArrayList<Pallet> palletList;

	public StoragePane(Database db) {
		super(db);

	}

	/**
	 * Create the left panel, containing the movie name list and the performance
	 * date list.
	 * 
	 * @return The left panel.
	 */
	public JComponent createLeftPanel() {
		nameListModel = new DefaultListModel();

		nameList = new JList(nameListModel);
		nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nameList.setPrototypeCellValue("123456789012");
		nameList.addListSelectionListener(new NameSelectionListener());
		JScrollPane p1 = new JScrollPane(nameList);
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1, 1));
		p.add(p1);

		return p;
	}

	/**
	 * Create the top panel, containing the fields with the performance data.
	 * 
	 * @return The top panel.
	 */
	public JComponent createTopPanel() {
		String[] texts = new String[8];
		texts[0] = "Cookie";
		texts[1] = "Baking date";
		texts[2] = "Order ID";
		texts[3] = "Blocked";
		texts[4] = "Location";
		texts[5] = "Customer";
		texts[6] = "Delivered";
		texts[7] = "Delivery Date";

		fields = new JTextField[8];
		for (int i = 0; i < fields.length; i++) {
			fields[i] = new JTextField(20);
			fields[i].setEditable(false);
		}

		JPanel input = new InputPanel(texts, fields);

		// JPanel p1 = new JPanel();
		// p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		// p1.add(new JLabel("Current customer: "));
		// currentUserNameLabel = new JLabel("");
		// p1.add(currentUserNameLabel);

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		// p.add(p1);
		p.add(input);
		return p;
	}

	/**
	 * Create the bottom panel, containing the book ticket-button and the
	 * message line.
	 * 
	 * @return The bottom panel.
	 */
	public JComponent createBottomPanel() {
		JButton[] buttons = new JButton[1];
		buttons[0] = new JButton("Block/unblock pallet");
		return new ButtonAndMessagePanel(buttons, messageLabel,
				new ActionHandler());
	}

	/**
	 * Perform the entry actions of this pane: clear all fields, fetch the movie
	 * names from the database and display them in the name list.
	 */
	public void entryActions() {
		clearMessage();
		// currentUserNameLabel.setText(CurrentUser.instance().getCurrentUserId());
		palletList = db.getPallets();
		fillNameList();
		clearFields();
	}

	/**
	 * Fetch movie names from the database and display them in the name list.
	 */
	public void fillNameList() {
		nameListModel.removeAllElements();
		palletList = db.getPallets();
		for (int i = 0; i < palletList.size(); i++) {
			nameListModel.addElement(palletList.get(i).getPID());
		}

	}

	/**
	 * Clear all text fields.
	 */
	private void clearFields() {
		for (int i = 0; i < fields.length; i++)
			fields[i].setText("");
	}

	/**
	 * A class that listens for clicks in the name list.
	 */
	class NameSelectionListener implements ListSelectionListener {
		/**
		 * Called when the user selects a name in the name list. Fetches
		 * performance dates from the database and displays them in the date
		 * list.
		 * 
		 * @param e
		 *            The selected list item.
		 */
		public void valueChanged(ListSelectionEvent e) {
			if (nameList.isSelectionEmpty())
				return;
			int orderIndex = nameList.getSelectedIndex();
			String[] palletInfo = db.getPalletInfo(palletList.get(orderIndex).getOID());
			fields[0].setText(palletList.get(orderIndex).getCookieName());
			fields[1].setText(palletList.get(orderIndex).getBakingDate());
			fields[2].setText(String.valueOf(palletList.get(orderIndex)
					.getOID()));
			fields[3].setText(palletList.get(orderIndex).getBlock() ? "Yes"
					: "No");
			fields[4].setText(palletList.get(orderIndex).getLocation());
			fields[5].setText(palletInfo[2]);
			fields[6].setText(palletInfo[1]);
			fields[7].setText(palletInfo[0]);
		}
	}

	/**
	 * A class that listens for button clicks.
	 */
	class ActionHandler implements ActionListener {
		/**
		 * Called when the user clicks the Book ticket button. Books a ticket
		 * for the current user to the selected performance (adds a booking to
		 * the database).
		 * 
		 * @param e
		 *            The event object (not used).
		 */
		public void actionPerformed(ActionEvent e) {
			if (nameList.isSelectionEmpty())
				return;
			
			int blockPallet = nameList.getSelectedIndex();
			if (!palletList.get(blockPallet).getBlock()) {
				if (db.blockPallet(palletList.get(blockPallet).getPID())) {
					displayMessage("Pallet has been blocked.");
				} else {
					displayMessage("Error occured.");
				}

			}else{
				if (db.unblockPallet(palletList.get(blockPallet).getPID())) {
					displayMessage("Pallet has been unblocked.");
				} else {
					displayMessage("Error occured.");
				}
			}
			
		}
	}
}
